package com.example.user_center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.user_center.model.domain.User;
import com.example.user_center.service.UserService;
import com.example.user_center.mapper.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.stream.Collectors;

import static com.example.user_center.constant.UserConstant.USER_LOGIN_STATE;

/**
* @author 21117
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-06-17 10:26:39
*/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    private UserMapper userMapper;
    private static final String SALT = "xingye";
    /**
     * 用户登录态键
     */


    /**
     * 用户注册
     * @param userAccount 用户账号
     * @param userPassword 用户密码
     * @param checkPassword 确认密码
     * @return 注册结果
     */
    @Override
    public long userRegister(String userAccount, String userPassword, String checkPassword) {
        //1.校验
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)){
            return -1;
        }
        if (userAccount.length()<4)
        {
            return -1;
        }
        if(userPassword.length()<8||checkPassword.length()<8)
        {
            return -1;
        }
                //账户不能包含特殊字符
        String regex = "^[a-zA-Z0-9\\s_]+$";
        if(!userAccount.matches(regex)){
            return -1;
        }
        //校验密码是否相等
        if(!userPassword.equals(checkPassword)){
            return -1;
        }
        //账户不能重复
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount",userAccount);
        long result = userMapper.selectCount(queryWrapper);
        if (result>0)
        {
            return -1;
        }
        //加密密码

        String newPassword = DigestUtils.md5DigestAsHex((SALT+userPassword).getBytes());
        //插入输入
        User user = new User();
        user.setUserAccount(userAccount);
        user.setPassword(newPassword);
        boolean saveResult = this.save(user);
        if (!saveResult)
        {
            return -1;
        }
        return user.getId();
    }

    @Override
    public User userLogin(String userAccount, String userPassword, HttpServletRequest request) {
        //判断用户名和密码是否为空
        if (StringUtils.isAnyBlank(userAccount, userPassword)){
            return null;
        }
        //判断用户名长度是否小于4
        if (userAccount.length()<4)
        {
            return null;
        }
        //判断密码长度是否小于8
        if(userPassword.length()<8)
        {
            return null;
        }
        //账户不能包含特殊字符
        String regex = "^[a-zA-Z0-9\\s_]+$";
        if(!userAccount.matches(regex)){
            return null;
        }

        //加密密码
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+userPassword).getBytes());
        //查询用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccount",userAccount);
        queryWrapper.eq("password",encryptPassword);
        User user = userMapper.selectOne(queryWrapper);
        if (user ==null){
            return null;
        }
        //用户脱敏
        User cleanUser = getSafetyUser(user);
        //记录用户登录态
        request.getSession().setAttribute(USER_LOGIN_STATE,cleanUser);
        return cleanUser;
    }
    @Override
    public User getSafetyUser(User originUser) {
        if (originUser == null) {
            return null;
        }
        User safetyUser = new User();
        safetyUser.setId(originUser.getId());
        safetyUser.setUserName(originUser.getUserName());
        safetyUser.setUserAccount(originUser.getUserAccount());
        safetyUser.setAvatarUrl(originUser.getAvatarUrl());
        safetyUser.setGender(originUser.getGender());
        safetyUser.setPhone(originUser.getPhone());
        safetyUser.setEmail(originUser.getEmail());
        safetyUser.setCreateTime(originUser.getCreateTime());
        safetyUser.setUserRole(originUser.getUserRole());
        safetyUser.setTags(originUser.getTags());
        return safetyUser;
    }

    @Override
    public List<User> searchUsersByTags(List<String> tagNameList){
        if(CollectionUtils.isEmpty(tagNameList)){
            throw new IllegalArgumentException("tagNameList cannot be empty");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        for (String tagName : tagNameList) {
            queryWrapper = queryWrapper.like("tags",tagName);
        }
        List<User> userList = userMapper.selectList(queryWrapper);
        return userList.stream().map(this::getSafetyUser).collect(Collectors.toList());
    }

}




